import { useUserStore } from '@/stores/user'
import axios from 'axios'

import { ElMessage } from 'element-plus'


const baseURL =import.meta.env.VITE_BASE_URL
// const baseURL ='https://anding.likegis.com/api/'
// const baseURL ='http://localhost:4091/'
const instance = axios.create({
  baseURL,
  timeout: 100000
})

instance.interceptors.request.use(
  (config) => {
    const userStore = useUserStore()
    if (userStore.token) {
      config.headers.Authorization = userStore.token
    }
    return config
  },
  (err) => Promise.reject(err)
)

instance.interceptors.response.use(
  (res) => {
    if(res.data.result==false){
      ElMessage({ message: res.data.msg})
      return Promise.reject(res.data)
    }else if(res.data||res.data.result===true){
      return res
    }
   
  },
  (err) => {
    ElMessage({ message: err.response.data.msg || '服务异常', type: 'error' })
    // if (err.response?.status === 500) {
    //   router.push('/login')
    // }
    return Promise.reject(err)
  }
)

export default instance
export { baseURL }